#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+10;
const int mod=10007;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
LL d[maxn],dp[maxn];
inline void pre(const char* s){
    LL n=strlen(s);
    memset(dp,0,n*sizeof(LL));
    memset(d,0,n*sizeof(LL));
    LL i=1;LL j=0;
    while(i<n){
        bool flag=false;
        if(str[i]==str[j]){
            dp[i]=(dp[d[i-1]]+1)%mod;
            d[i++]=++j;
        }else{
            if(j) j=d[j-1];
            else flag=true;
        }
        if(flag){
            dp[i]=(dp[d[i-1]])%mod;
            i++;
        }
    }
}
inline LL solve(const char* s){
    pre(s);
    LL n=strlen(s);
    LL ans=0;
    for(int i=0;i<n;i++){
        ans=(ans+dp[i]+1)%mod;
    }
    return ans;
}
int main(void){
    LL t;t=read();
    while(t--){
        LL n;n=read();
        scanf("%s",str);
        printf("%d\n",solve(str));
    }
   	return 0;
}
